import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @Author: Lenovo
 * @Date: 2024/10/20 22:42
 * @description:
 */
public class Sort {
    // 插入排序
    public static void insertSort(int[] array){
        for(int i = 1;i<array.length;i++){
            int tmp = array[i];
            int j = i-1;
            for (;j>=0;j--){
                if (array[j] > tmp){
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    // 希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while (gap > 1){
            // gap 不能大于等于 1，因为如果 gap = 1,此时进入循序，
            // gap = 0，那么下面的shell 进入到死循环
            gap /= 2;
            shell(array,gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for(int i = 0;i<array.length;i+=gap){
            int tmp = array[i];
            int j = i - gap;
            for (;j>=0;j-=gap){
                if (array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    array[j+gap] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    // 选择排序
    public static void selectSort2(int[] array){
        int n = array.length;
        for(int i = 0;i<n;i++){
            int minIndex = i;
            for(int j = i+1;j < n;j++){
                if (array[minIndex] > array[j]){
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void selectSort(int[] array){
        int left = 0,right = array.length - 1;
        while (left <= right){
            int maxIndex = left,minIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (array[i] > array[maxIndex]){
                    maxIndex = i;
                }
                if (array[i] < array[minIndex]){
                    minIndex = i ;
                }
            }
            swap(array,minIndex,left);
            if (maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }
    public static void heapSort(int[]array){
        createHeap(array);
        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            shitDown(array,0,end);
            end--;
        }
    }

    private static void shitDown(int[] array, int parent, int length) {
        int child = 2 * parent + 1;
        while (child < length){
            if (child + 1 < length && array[child] < array[child + 1]){
                child++;
            }
            if (array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    private static void createHeap(int[] array) {
        for(int parent = (array.length - 1 - 1)/2;parent >= 0;parent--){
            shitDown(array,parent, array.length);
        }
    }
    public static void bubbleSort(int[] array){
        for (int i = 0;i<array.length-1;i++){
            boolean flg = false;
            for (int j = 0;j<array.length-i-1;j++){
                if (array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (!flg){
                break;
            }
        }
    }
    public static void quickSort(int[] array){
        //quick(array,0,array.length-1);
        quickNor(array,0,array.length-1);
    }
    public static void quickNor(int[] array,int start,int end) {
        Stack<Integer> stack = new Stack<>();
        int pivot = partition(array,start,end);
        if (pivot > start + 1){
            stack.push(start);
            stack.push(pivot-1);
        }
        if (pivot < end - 1){
            stack.push(pivot+1);
            stack.push(end);
        }
        while (!stack.isEmpty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array,start,end);
            if (pivot > start + 1){
                stack.push(start);
                stack.push(pivot-1);
            }
            if (pivot < end - 1){
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end){
            return;
        }
        if (end - start + 1 <= 5){
            insertSortRange(array,start,end);
            return;
        }
        int midIndex = getMiddle(array,start,end);
        swap(array,midIndex,start);
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static void insertSortRange(int[] array, int start, int end) {
        for(int i = start + 1;i <= end;i++){
            int tmp = array[i];
            int j = i - 1;
            for (;j >= start; j--){
                if (array[j] > tmp){
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static int getMiddle(int[]array,int left,int right) {
        int mid = (left + right)/2;
        if (array[left] < array[right]){
            if (array[mid] < array[left]){
                return left;
            } else if (array[right] < array[mid] ) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[right] < array[mid]){
                return right;
            }else if (array[mid] > array[left]){
                return left;
            }else {
                return mid;
            }
        }
    }
    /*
    挖坑法
     */
    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp ){
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    /*
    Hoare 版本的查找基准值
     */
    private static int partitionHoare(int[] array, int left, int right) {
        int tmpleft = left;
        int pivot = array[left];
        while (left < right){
            while (left < right && array[right] > pivot){
                right--;
            }
            while (left < right && array[left] < pivot){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,tmpleft,left);
        return left;
    }
    public static void mergeSort(int[]array){
        //mergeSortTmp(array,0,array.length-1);
        mergeSortNor(array);
    }

    private static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length){
            for (int i = 0;i<array.length;i = i + gap * 2){
                int left = i;
                int mid = left + gap - 1;
                if (mid > array.length){
                    mid = array.length - 1;
                }
                int right = mid + left;
                if (right > array.length){
                    right = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    private static void mergeSortTmp(int[] array, int left, int right) {
        if (left > right) {
            return;
        }
        int mid = (left + right)/2;
        // 分解操作：递归
        mergeSortTmp(array,left,mid);
        mergeSortTmp(array,mid+1,right);
        // 合并：合并两个有序数组
        merge(array,left,mid,right);

    }

    private static void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int s1 = left,e1 = mid,s2 = mid+1,e2 = right;
        while (s1 <= mid && s2 <= right){
            if (array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid){
            tmp[k++] = array[s1++];
        }
        while (s2 <= right){
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k;i++){
            array[i+left] = tmp[k];
        }
    }

}
